package com.xin.data.simbapesa.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatRatingBar;

import com.xin.data.simbapesa.R;
import com.xin.data.simbapesa.utils.ResUtils;

public class SimbaRatingBar extends AppCompatRatingBar {


    private int starSpacing;
    private int starSize;
    private Drawable emptyStarDrawable;
    private Drawable halfStarDrawable;
    private Drawable fullStarDrawable;


    public SimbaRatingBar(@NonNull Context context) {
        super(context);
        init(null);
    }

    public SimbaRatingBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public SimbaRatingBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }


    private void init(AttributeSet attrs) {

        emptyStarDrawable = AppCompatResources.getDrawable(getContext(), R.mipmap.stars1);
        halfStarDrawable = AppCompatResources.getDrawable(getContext(), R.mipmap.stars2);
        fullStarDrawable = AppCompatResources.getDrawable(getContext(), R.mipmap.stars2);


        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomRatingBar);

        starSpacing = a.getDimensionPixelSize(R.styleable.CustomRatingBar_starSpacing, 0);
        starSize = a.getDimensionPixelSize(R.styleable.CustomRatingBar_starSize,ResUtils.d(R.dimen.dp_24));


        emptyStarDrawable.setBounds(0, 0, starSize, starSize);
        halfStarDrawable.setBounds(0, 0, starSize, starSize);
        fullStarDrawable.setBounds(0, 0, starSize, starSize);

        a.recycle();


        setProgressDrawable(null);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int numStars = getNumStars();
        int width = numStars * starSize + (numStars - 1) * starSpacing;
        int height = starSize;

        setMeasuredDimension(width, height);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        int numStars = getNumStars();
        float rating = getRating();
        int fullStars = (int) rating;
        boolean hasHalfStar = (rating - fullStars) >= 0.5f;

        for (int i = 0; i < numStars; i++) {
            int left = i * (starSize + starSpacing);

            if (i < fullStars) {
                drawStar(canvas, fullStarDrawable, left);
            } else if (i == fullStars && hasHalfStar) {
                drawStar(canvas, halfStarDrawable, left);
            } else {
                drawStar(canvas, emptyStarDrawable, left);
            }
        }
    }

    private void drawStar(Canvas canvas, Drawable drawable, int left) {
        canvas.save();
        canvas.translate(left, 0);
        drawable.draw(canvas);
        canvas.restore();
    }

//    @SuppressLint("ClickableViewAccessibility")
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        if (!isIndicator()) {
//            if (event.getAction() == MotionEvent.ACTION_DOWN) {
//                updateRating(event.getX());
//                return true;
//            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
//                updateRating(event.getX());
//                return true;
//            }
//        }
//        return super.onTouchEvent(event);
//    }

//    private void updateRating(float x) {
//        int starIndex = (int) (x / (starSize + starSpacing));
//        float rating = starIndex + 1;
//
//
//        float positionInStar = x - starIndex * (starSize + starSpacing);
//        if (positionInStar > starSize) {
//            rating = starIndex + 1;
//        } else if (positionInStar > starSize / 2) {
//            rating = starIndex + 0.5f;
//        } else {
//            rating = starIndex;
//        }
//
//        setRating(rating);
//    }


}
